package com.insurance.handler.exception;

import com.insurance.domain.ResponseResult;
import com.insurance.domain.enums.AppHttpCodeEnum;
import com.insurance.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author qcjstudy
 * @create 2022-11-30 20:50
 * @ControllerAdvice 表示当Controller的异常都会转到这里处理，@RestControllerAdvice 表示异常在这里返回响应给前端，而不再controller响应了
 */
@RestControllerAdvice//相当于@ResponseBody和@ControllerAdvice
@Slf4j //打印日志
public class GlobalExceptionHandler {

    @ExceptionHandler(SystemException.class)
    public ResponseResult systemExceptionHandler(SystemException e){
        //打印异常信息
        log.error("出现了异常！{}",e);
        //从异常对象中获取提示信息封装返回
        return ResponseResult.errorResult(e.getCode(),e.getMsg());
    }

    /**
     * 系统异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult systemExceptionHandler(Exception e){
        //打印异常信息
        log.error("出现了异常！{}",e);
        //从异常对象中获取提示信息封装返回
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(),e.getMessage());
    }
}
